package com.boolib.service.impl;

import com.boolib.commons.Const;
import com.boolib.commons.entity.ServerResponse;
import com.boolib.commons.util.ServerResponseUtil;
import com.boolib.dao.ShopCategoryMapper;
import com.boolib.pojo.ShopCategory;
import com.boolib.pojo.ShopCategoryExample;
import com.boolib.service.ICategoryService;
import com.boolib.tree.TreeModelHandlerImpl;
import com.boolib.tree.exception.IsTopCategoryException;
import com.boolib.tree.exception.NotFoundItemException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private ShopCategoryMapper shopCategoryMapper;



    @Override
    public ServerResponse addCategory(ShopCategory shopCategory, String createBy){

        ServerResponse serverResponse = ServerResponseUtil.argumentCheck(shopCategory.getCategoryName(),shopCategory.getParentId(),shopCategory,createBy);

        if(!serverResponse.isSuccess()){
            return  serverResponse;
        }

        serverResponse = checkName(shopCategory.getCategoryName());

        if(!serverResponse.isSuccess()) return  serverResponse;


        serverResponse = checkParentId(shopCategory.getParentId());

        if(!serverResponse.isSuccess())return  serverResponse;


        shopCategory.setId(null);
        shopCategory.setCreateBy(createBy);
        shopCategory.setCreateTime(new Date());
        shopCategory.setUpdateTime(new Date());



        if(shopCategoryMapper.insertSelective(shopCategory)>0){

            log.warn("管理员"+createBy+":管理员添加了"+shopCategory.getCategoryName()+"分类");
            return ServerResponseUtil.insertSuccess("分类添加成功");
        }
        return ServerResponseUtil.insertFail("分类添加失败");
    }


    @Override
    public ServerResponse updateCategory(ShopCategory shopCategory, String updateBy){

        ServerResponse serverResponse  = ServerResponseUtil.argumentCheck(shopCategory.getId(),updateBy);

        if(!serverResponse.isSuccess()) return  serverResponse;

        String categoryName = shopCategory.getCategoryName();

        if(categoryName != null){
            serverResponse = checkName(categoryName);
            if(!serverResponse.isSuccess()) return  serverResponse;
        }

        Integer parentId = shopCategory.getParentId();

        if(parentId != null ){
            serverResponse = checkParentId(parentId);
            if(!serverResponse.isSuccess()) return  serverResponse;
        }


        shopCategory.setCreateBy(null);
        shopCategory.setCreateTime(null);
        shopCategory.setStatus(null);
        shopCategory.setUpdateBy(updateBy);
        shopCategory.setUpdateTime(new Date());

        if(shopCategoryMapper.updateByPrimaryKeySelective(shopCategory)>0){
            log.warn("管理员"+updateBy+":管理员更新了"+shopCategory.getCategoryName()+"分类");
            return ServerResponseUtil.updateSuccess("分类更新成功");
        }
        return ServerResponseUtil.updateFail("分类更新失败");

    }














    @Override
    public ServerResponse selectAll(){
        return ServerResponse.createBySuccess(shopCategoryMapper.selectAll());
    }



    @Override
    public ServerResponse selectCategoryTree(){
        TreeModelHandlerImpl treeModelHandler = new TreeModelHandlerImpl(shopCategoryMapper.selectAll());
        return ServerResponse.createBySuccess(treeModelHandler.getAfterNode());
    }


    @Override
    public ServerResponse selectBrothers(Integer domId){
        ServerResponse serverResponse = ServerResponseUtil.argumentCheck(domId);

        if(!serverResponse.isSuccess()) return  serverResponse;

        TreeModelHandlerImpl treeModelHandler = new TreeModelHandlerImpl((List)selectAll().getData());
        try {
            return ServerResponse.createBySuccess(treeModelHandler.findBrothers(domId));
        } catch (NotFoundItemException e) {
            return  ServerResponse.createBySuccess(null);
        }
    }



    @Override
    public ServerResponse selectParent(Integer domId){
        ServerResponse serverResponse = ServerResponseUtil.argumentCheck(domId);

        if(!serverResponse.isSuccess()) return  serverResponse;


        TreeModelHandlerImpl treeModelHandler = new TreeModelHandlerImpl((List)selectAll().getData());
        try {
            return ServerResponse.createBySuccess(treeModelHandler.findParent(domId));
        } catch (NotFoundItemException e) {
            return  ServerResponse.createBySuccess(null);
        } catch (IsTopCategoryException e) {
            return ServerResponse.createBySuccess(null);
        }
    }


    /**
     * 面包屑
     * @param domId
     * @return
     */

    @Override
    public ServerResponse selectParents(Integer domId){
        ServerResponse serverResponse = ServerResponseUtil.argumentCheck(domId);

        if(!serverResponse.isSuccess()) return  serverResponse;



        TreeModelHandlerImpl treeModelHandler = new TreeModelHandlerImpl((List)selectAll().getData());
        try {
            return ServerResponse.createBySuccess(treeModelHandler.findParents(domId));
        } catch (NotFoundItemException e) {
            return  ServerResponse.createBySuccess(null);
        }
    }

    @Override
    public ServerResponse selectChildren(Integer domId) {
        ServerResponse serverResponse = ServerResponseUtil.argumentCheck(domId);

        if(!serverResponse.isSuccess()) return  serverResponse;


        TreeModelHandlerImpl treeModelHandler = new TreeModelHandlerImpl((List)selectAll().getData());
        try {
          return  serverResponse.createBySuccess(  treeModelHandler.findChildren(domId));
        } catch (NotFoundItemException e) {
            return  ServerResponse.createBySuccess(null);
        }


    }


    /**
     * 商品分类禁用
     * @param categoryId
     * @return
     */
    @Override
    public ServerResponse forbid(Integer categoryId,String updateBy) {

        ServerResponse serverResponse = ServerResponseUtil.argumentCheck(categoryId,updateBy);
        if(!serverResponse.isSuccess()) return serverResponse;


        ShopCategory shopCategory = new ShopCategory();

        shopCategory.setId(categoryId);


        ShopCategory result =  shopCategoryMapper.selectOne(shopCategory);

        serverResponse = ServerResponseUtil.resultCheck(result,ShopCategory.class);

        if(!serverResponse.isSuccess()) return serverResponse;

        shopCategory.setStatus(Const.Category.FORBID);

        shopCategory.setUpdateBy(updateBy);

        shopCategory.setUpdateTime(new Date());



        if(shopCategoryMapper.updateByPrimaryKeySelective(shopCategory)>0){
            log.warn("管理员"+updateBy+":管理员禁用了"+result.getCategoryName()+"分类");
            return ServerResponseUtil.updateSuccess("分类禁用成功");
        }

        return ServerResponseUtil.updateFail("分类禁用失败");
    }



    /**
     * 商品分类恢复
     * @param categoryId
     * @return
     */
    @Override
    public ServerResponse recover(Integer categoryId,String updateBy) {

        ServerResponse serverResponse = ServerResponseUtil.argumentCheck(categoryId,updateBy);

        if(!serverResponse.isSuccess()) return serverResponse;

        ShopCategory shopCategory = new ShopCategory();

        shopCategory.setId(categoryId);

        ShopCategory result =  shopCategoryMapper.selectOne(shopCategory);

        serverResponse = ServerResponseUtil.resultCheck(result,ShopCategory.class);

        if(!serverResponse.isSuccess()) return serverResponse;

        shopCategory.setStatus(Const.Category.NORMAL);

        shopCategory.setUpdateBy(updateBy);

        shopCategory.setUpdateTime(new Date());

        if(shopCategoryMapper.updateByPrimaryKeySelective(shopCategory)>0){
            log.warn("管理员"+updateBy+":管理员恢复了"+result.getCategoryName()+"分类");
            return ServerResponseUtil.updateSuccess("分类恢复成功");
        }

        return ServerResponseUtil.updateFail("分类恢复失败");
    }






    @Override
    public ServerResponse selectOneNormal(Integer id) {

        ServerResponse serverResponse = ServerResponseUtil.argumentCheck(id);

        if(!serverResponse.isSuccess()) return serverResponse;

        ShopCategory shopCategory = new ShopCategory();

        shopCategory.setId(id);

        shopCategory.setStatus(Const.Category.NORMAL);

        ShopCategory shopCategory1 = shopCategoryMapper.selectOne(shopCategory);

        serverResponse = ServerResponseUtil.resultCheck(shopCategory1,ShopCategory.class);

        if(!serverResponse.isSuccess()) return  serverResponse;
        return ServerResponse.createBySuccess(shopCategory);
    }

    @Override
    public ServerResponse selectAllNormal(int pageNum, int pageSize) {

        ShopCategoryExample shopCategoryExample = new ShopCategoryExample();

        ShopCategoryExample.Criteria criteria = shopCategoryExample.createCriteria();

        criteria.andStatusEqualTo(0);


        PageHelper.startPage(pageNum,pageSize).doSelectPageInfo(()->shopCategoryMapper.selectByExample(shopCategoryExample));

        PageInfo pageInfo = new PageInfo();


        return ServerResponse.createBySuccess(pageInfo);
    }




    @Override
    public ServerResponse selectAllAbnormal(int pageNum, int pageSize) {

        ShopCategoryExample shopCategoryExample = new ShopCategoryExample();

        ShopCategoryExample.Criteria criteria = shopCategoryExample.createCriteria();

        criteria.andStatusGreaterThan(0);


        PageHelper.startPage(pageNum,pageSize);

        PageInfo pageInfo = new PageInfo(shopCategoryMapper.selectByExample(shopCategoryExample));

        return ServerResponse.createBySuccess(pageInfo);
    }



    private ServerResponse checkName(String name){

        ShopCategory shopCategory = new ShopCategory();

        shopCategory.setCategoryName(name);

        if(shopCategoryMapper.select(shopCategory).size()>0){
            return ServerResponse.createByError(Const.Category.ERRORSTATUS.CATEGORYEXIST.getCode(),Const.Category.ERRORSTATUS.CATEGORYEXIST.getMessage());
        }

        return ServerResponse.createBySuccess();
    }



    private ServerResponse checkParentId(Integer parentId){


        if(parentId.equals(0)){return ServerResponse.createBySuccess();}

        ShopCategory shopCategory = new ShopCategory();

        shopCategory.setId(parentId);

        shopCategory.setStatus(Const.Category.NORMAL);

        if(shopCategoryMapper.selectOne(shopCategory)!=null){
            return ServerResponse.createByError(Const.Category.ERRORSTATUS.CATEGORYEXIST.getCode(),Const.Category.ERRORSTATUS.CATEGORYEXIST.getMessage());
        }

        return ServerResponse.createBySuccess();
    }














}
